<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{

	//用户添加da
    public function getAdd()
    {
    	//解析模板
    	return view('Admin.user.add');
    }

    //用户添加执行
    public function postInsert(Request $request)
    {
    	// dd($request->all());
    	   $this->validate($request, [
		        'username' => 'required|max:15',
		        'password' => 'required',
		        'repassword' => 'required|same:password',
                'age' => 'required|regex:{\d{1,3}}|max:3',
		        'email' => 'required|email',
		        'phone' => 'required|regex:{1\d{10}}'
            ],[
		    	'username.required' => '用户名不能为空',
		    	'username.max' => '用户名必须小于15个字符',
		    	'password.required' => '密码不能为空',
		    	'repassword.required' => '确认密码不能为空',
		    	'repassword.same' => '两次密码不一致',
                'age.required' => '年龄不能为空',
                'age.regex' => '年龄格式不正确,请输入数字',
                'age.max' => '年龄不能超过3位数',
		    	'email.required' => '邮箱不能为空',
		    	'email.email'=>'邮箱格式不正确',
		    	'phone.required' => '手机号不能为空',
		    	'phone.regex' => '手机号格式不正确'
		    ]);
            //随机文件名
            $name = md5(time()+rand(1,999999));
            //获取后缀名
            $su = $request->file('pic')->getClientOriginalExtension();

            $arr = array('jpg','png','jpeg','tiff','gif');
            //判断
            if(!in_array($su,$arr)){
                // die('非法上传');
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="../user/add";
                </script>';
            }
            //将文件移动到指定目录下
            $request->file('pic')->move('./upload/', $name.'.'.$su);
        
            $arr = $request->only(['username','password','age','email','phone','sex','edu','qx']);
            $arr['pic'] = $name.'.'.$su;  
            $arr['token'] = str_random(50);
			$arr['password'] = Hash::make($arr['password']);
            $arr['rtime'] = time();
			// var_dump($arr);exit;
			//添加
    	   $res = DB::table('user')->insert($arr);

    	   //判断添加是否成功
    	   if($res){
    	   		return redirect('/admin/user/index')->with('success','用户添加成功');
    	   }else{
    	   		return back()->with('error','用户添加失败');
    	   }
    }	

	//用户列表
	public function getIndex(Request $request)
	{
		// dd($request->all());
        // $users = DB::table('user')->get();
		//获取 搜索
		$num = $request->input('num',10);
		if($request->input('keywords')){
			$users = DB::table('user')->where('username','like','%'.$request->input('keywords').'%')->paginate($num);	
		}else{
			$users = DB::table('user')->paginate($num);
		}
		//查询时使用闭包
		// $users = DB::table('user')->where(function($query) use($request){
		// 	if($request->input['uname']){
		// 		$query->where('username','like','%'.$request->input('uname').'%');
		// 	}
		// })->paginate($num);	
		$data = $request->all();
		// var_dump($data);
		return view('Admin.user.index',['users'=>$users,'request'=>$data]);
	}

	//修改
	public function getEdit($id)
	{
		//查询
		$users = DB::table('user')->where('id',$id)->first();
		// dd($users);
		//解析模板
		return view('admin.user.edit',['user'=> $users]);
	}

	//执行修改
	public function postUpdate(Request $request)
	{
        // dd($request->all());
        // dd($request->pic);
        // dd($request->hasFile('pic'));
        $this->validate($request, [
            'username' => 'required|max:15',
            'email' => 'required|email',
            'phone' => 'required|regex:{1\d{10}}'
        ],[
            'username.required' => '用户名不能为空',
            'username.max' => '用户名必须小于15个字符',
            'email.required' => '邮箱不能为空',
            'email.email'=>'邮箱格式不正确',
            'phone.required' => '手机号不能为空',
            'phone.regex' => '手机号格式不正确'
        ]);
        // dd($data['pic']);
        // dd($request->all());
        $id = $request->only('id');
        $data = $request->only(['username','email','phone','sex','edu','qx']);
        //检测文件是否上传
        if($request->hasFile('pic')){
            //如果上传了
            //随机文件名
            $name = md5(time()+rand(1,999999));
            //获取后缀名
            $su = $request->file('pic')->getClientOriginalExtension();
            $arr = array('jpg','png','jpeg','tiff');
            //判断
            if(!in_array($su,$arr)){
                // die('非法上传');
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="../user/edit";
                </script>';
            }
            //将文件移动到指定目录下
            $request->file('pic')->move('./upload/', $name.'.'.$su);
            $data['pic'] = $name.'.'.$su;
        }else{
            $data['pic'] = $request->pic;
        }
        // dd($data);
        //数据库修改
		$res = DB::table('user')->where('id',$id)->update($data);
        // dd($res);

		//判断
		if($res){
			return redirect('/admin/user/index')->with('success','用户修改成功');
		}else{
			return back()->with('error','用户修改失败');
		}
	}

	//删除
	public function postDelete(Request $request)
	{
		// dd($request);
        //删除
		// $res = DB::table('user')->where('id',$id)->delete();
		// //判断
		// if($res){
		// 	return redirect('/admin/user/index')->with('success','删除成功');
		// }else{
		// 	return back()->with('error','删除失败');
		// }
		$id = $request->input('id');
		$res = DB::table('user')->where('id',$id)->delete();
		if($res){
			echo 1;
		}else{
			echo 2;
		}
	}
}
